﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsCurrencyDAL.cs
/// 作  者：黄纲
/// 时  间：2006.11.05
/// 说  明：ClsCurrencyDAL类用于币别信息相关处理
/// </summary>
public class ClsCurrencyDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有的币别信息
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回币别信息数据集</returns>
    public DataSet GetCurrencys(string Co_ID, string User_ID, string Ds_ID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@co_id", Co_ID);
        dat.SelectCommand.Parameters.Add("@user_id", User_ID);
        dat.SelectCommand.Parameters.Add("@ds_id", Ds_ID);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 搜索币别信息
    /// </summary>
    /// <param name="Key">搜索关键字</param>
    /// <returns>返回搜索信息数据集</returns>
    public DataSet SearchCurrencys(string Key)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchCurrencys", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@key", Key);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 检验币别编号是否存在
    /// </summary>
    /// <param name="Cr_ID">币别编号</param>
    /// <returns>返回－1检验失败，0编号不存在，大于0存在</returns>
    public int CheckUpCurrencyID(string Cr_ID)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_CheckUpCurrencyID", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@cr_id", SqlDbType.NVarChar, 20);
        com.Parameters["@cr_id"].Value = Cr_ID;

        try
        {
            con.Open();

            return (int)com.ExecuteScalar();
        }
        catch
        {
            return -1;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 添加币别信息
    /// </summary>
    /// <param name="Cr_ID">币别编号</param>
    /// <param name="Cr_Name">币别名称</param>
    /// <param name="Cr_Rate">币别汇率</param>
    /// <param name="Cr_Mode">折算方式</param>
    /// <param name="Cr_Remark">备注</param>
    /// <param name="Cr_Oper">录入人</param>
    /// <returns>返回添加是否成功</returns>
    public Boolean AddCurrency(string Cr_ID, string Cr_Name, decimal Cr_Rate, string Cr_Mode, string Cr_Remark, string Cr_Oper)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddCurrency", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@cr_id", SqlDbType.NVarChar, 20);
        com.Parameters["@cr_id"].Value = Cr_ID;
        com.Parameters.Add("@cr_name", SqlDbType.NVarChar, 60);
        com.Parameters["@cr_name"].Value = Cr_Name;
        com.Parameters.Add("@cr_rate", SqlDbType.Decimal, 17);
        com.Parameters["@cr_rate"].Value = Cr_Rate;
        com.Parameters.Add("@cr_mode", SqlDbType.NVarChar, 1);
        com.Parameters["@cr_mode"].Value = Cr_Mode;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 60);
        com.Parameters["@oper"].Value = Cr_Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Cr_Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改币别信息
    /// </summary>
    /// <param name="Cr_ID">币别编号</param>
    /// <param name="Cr_Name">币别名称</param>
    /// <param name="Cr_Rate">币别汇率</param>
    /// <param name="Cr_Mode">折算方式</param>
    /// <param name="Cr_Remark">备注</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpDateCurrency(string Cr_ID, string Cr_Name, decimal Cr_Rate, string Cr_Mode, string Cr_Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateCurrency", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@cr_id", SqlDbType.NVarChar, 20);
        com.Parameters["@cr_id"].Value = Cr_ID;
        com.Parameters.Add("@cr_name", SqlDbType.NVarChar, 60);
        com.Parameters["@cr_name"].Value = Cr_Name;
        com.Parameters.Add("@cr_rate", SqlDbType.Decimal, 17);
        com.Parameters["@cr_rate"].Value = Cr_Rate;
        com.Parameters.Add("@cr_mode", SqlDbType.NVarChar, 1);
        com.Parameters["@cr_mode"].Value = Cr_Mode;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Cr_Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废币别信息
    /// </summary>
    /// <param name="Cr_ID">币别编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteCurrency(string Cr_ID, int Is_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteCurrency", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@cr_id", SqlDbType.NVarChar, 20);
        com.Parameters["@cr_id"].Value = Cr_ID;
        com.Parameters.Add("@is_visible", SqlDbType.Int, 1);
        com.Parameters["@is_visible"].Value = Is_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认币别信息
    /// </summary>
    /// <param name="Cr_ID">币别编号</param>
    /// <param name="AuditStat">确认状态</param>
    /// <param name="Auditor">确认人</param>
    /// <returns>返回确认是否成功</returns>
    public Boolean AuditStatCurrency(string Cr_ID, int AuditStat, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditStatSetCurrency", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@cr_id", SqlDbType.NVarChar, 20);
        com.Parameters["@cr_id"].Value = Cr_ID;
        com.Parameters.Add("@is_audit", SqlDbType.Int);
        com.Parameters["@is_audit"].Value = AuditStat;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}
